<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            function ArrayList() {
                this.array = [];
                ArrayList.prototype.insert = function (value) {
                    this.array.push(value);
                };
                ArrayList.prototype.toString = function () {
                    return this.array.join('-');
                };
                // 冒泡排序：
                ArrayList.prototype.bubbleSort = function () {
                    var length = this.array.length;
                    for (let j = length - 1; j >= 0; j--) {
                        for (let i = 0; i < j; i++) {
                            if (this.array[i] > this.array[j]) {
                                var temp = this.array[i];
                                this.array[i] = this.array[j];
                                this.array[j] = temp;
                            }
                        }
                    }
                };
                // 选择排序:从第一个开始排序
                ArrayList.prototype.selectSort = function () {
                    var length = this.array.length;
                    for (let i = 0; i < length-1; i++) {
                        //var min = i;
                        for (let j = i+1; j < length; j++) {
                            if (this.array[i] > this.array[j]) {
                                var temp = this.array[i];
                                this.array[i] = this.array[j];
                                this.array[j] = temp;
                            }
                        }
                    }
                };
            }
            let arrayList = new ArrayList();
            arrayList.insert(140);
            arrayList.insert(11);
            arrayList.insert(122);
            arrayList.insert(13);
            arrayList.insert(12);
            arrayList.selectSort();
            console.log(arrayList.toString());
        </script>
    </body>
</html>
